package com.wlt.security;

import com.alibaba.fastjson2.JSON;
import com.wlt.result.BaseResult;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import java.io.IOException;

/**
 * 未登录处理器
 */
public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint
{
    @Override
    public void commence (
        HttpServletRequest request,
        HttpServletResponse response,
        AuthenticationException authException) throws IOException, ServletException
    {
        BaseResult baseResult = new BaseResult(401, "用户未登录", null);
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(baseResult));
    }
}
